package com.net.wanjian.networkhospitalmanager.fragment.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.net.wanjian.networkhospitalmanager.netstatus.NetChangeObserver;
import com.net.wanjian.networkhospitalmanager.netstatus.NetStateReceiver;
import com.net.wanjian.networkhospitalmanager.netstatus.NetUtils;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Description:
 * Created by lidongcai on 2017/4/22.
 * Email:  lidongcaiocc@163.com
 */

public abstract class BaseFragment extends Fragment {
    protected String TAG  = getClass().getSimpleName();
    private NetChangeObserver mNetChangeObserver = null;

    protected Context mContext;
    private View rootView;
    private Unbinder unbinder;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mNetChangeObserver = new NetChangeObserver(){
            @Override
            public void onNetConnected(NetUtils.NetType type) {
                super.onNetConnected(type);
                onNetworkConnected(type);
            }

            @Override
            public void onNetDisConnect() {
                super.onNetDisConnect();
                onNetworkDisConnected();
            }
        };

        NetStateReceiver.registerObserver(mNetChangeObserver);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null){
            rootView = inflater.inflate(getLayoutId(),container,false);
            mContext = getContext();
        }
        ButterKnife.bind(this,rootView);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        NetStateReceiver.registerNetworkStateReceiver(mContext);
        initView();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        initData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (rootView != null && rootView.getParent() != null){
            ((ViewGroup)rootView.getParent()).removeView(rootView);
        }
//        unbinder.unbind();

        NetStateReceiver.removeRegisterObserver(mNetChangeObserver);
    }

    public void openActivity(Class<?> c,Bundle b){
        Intent intent = new Intent(mContext, c);
        if (null != b){
            intent.putExtras(b);
        }
        startActivity(intent);
    }

    public void openActivity(Class<?> c){
        openActivity(c,null);
    }

    protected abstract void initView();
    protected abstract void onNetworkConnected(NetUtils.NetType type);
    protected abstract void onNetworkDisConnected();
    protected abstract int getLayoutId();
    protected abstract void initData();


}
